// https://github.com/youzan/vant-weapp/blob/dev/packages/dialog/dialog.ts
function getContext() {
  const pages = getCurrentPages()
  return pages[pages.length - 1]
}

export const $showModal = (options: ModalOptions) => {
  return new Promise<'confirm' | 'cancel'>((resolve) => {
    getContext().selectComponent("#modal").show({
      title: options.title,
      content: options.content,
      success: () => {
        options.success && options.success()
        resolve('confirm')
      },
      error: () => {
        options.error && options.error()
        resolve('cancel')
      },
    })
  })
}
